package com.cssw.fyzb.tps.api.sms;

import com.cssw.fyzb.tps.exception.BizException;
import com.cssw.fyzb.tps.service.sms.AimSms;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 退出登录
 *
 * @author Feng Chen
 */
@Function(description = "发送短信", parameters = {
        @Parameter(name = "mobile", type = Type.String, description = "手机号", required = true),
        @Parameter(name = "message", type = Type.String, description = "短信内容", required = true)
})
@Component
public class SendSms implements Action {

    @Autowired
    private AimSms aimSms;

    private static final Logger logger = LoggerFactory.getLogger(SendSms.class);

    @Override
    public Response execute(Request request) throws Exception {
        String mobile = request.getString("mobile");
        String message = request.getString("message");
        try {
            aimSms.send(mobile, message);
            return Response.success();
        } catch (Exception e) {
            logger.error("", e);
            if (e instanceof BizException) {
                BizException se = (BizException) e;
                return Response.fail(se.getErrMsg(), se.getErrCode());
            }
            return Response.fail("退出登录失败");
        }
    }

}
